package tsb.com.html5.controller.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import tsb.com.html5.controller.base.BaseController;

@Controller
@RequestMapping("/captcha")
public class CaptchaController extends BaseController {

	@RequestMapping(value = "/verify-captcha", method = RequestMethod.GET)
	protected @ResponseBody boolean verifyCaptcha(@RequestParam String challenge,
			@RequestParam String response, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		boolean valid = true;
		try {
			String remoteAddr = req.getRemoteAddr();
			ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
			reCaptcha.setPrivateKey("6LdUV8USAAAAACc9N7eKj05CKnYzVcZT92ky6p4z");

			ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(
					remoteAddr, challenge, response);

			if (reCaptchaResponse.isValid()) {
				valid = true;
			} else {
				valid = false;
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return valid;
	}

}
